const Flatten = require("../template")

function testFlattenPerformance() {
  console.log(
    "1. 大量数据扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, i + 1, i + 2])
      const start = performance.now()
      Flatten.flatten(arr)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "2. 大量数据深度扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => [
        [i, [i + 1, [i + 2, i + 3]]]
      ])
      const start = performance.now()
      Flatten.flattenDeep(arr)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "3. 大量数据指定深度扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenDepth(arr, 2)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "4. 大量数据不可变扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenImmutable(arr)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "5. 大量数据带过滤扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenWithFilter(arr, (item) => item % 2 === 0, 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "6. 大量数据带映射扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenWithMap(arr, (item) => item * 2, 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "7. 大量数据带去重扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i, i + 1]])
      const start = performance.now()
      Flatten.flattenWithUnique(arr, 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "8. 大量数据带排序扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenWithSort(arr, (a, b) => a - b, 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "9. 大量数据带分组扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenWithGroup(arr, (item) => item % 2 === 0 ? 'even' : 'odd', 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "10. 大量数据带统计扁平化性能测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const start = performance.now()
      Flatten.flattenWithStats(arr, 1)
      const end = performance.now()
      return (end - start) < 100 // 100ms内完成
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(arr) {
          const start = performance.now()
          const flattened = Flatten.flatten(arr)
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: flattened.length * this.multiplier
          }
        }
      }
      
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      const result = perfObj.processPerformance(arr)
      
      return result.time < 100 && result.result > 0
    })()
  )

  console.log(
    "12. 扁平化性能一致性测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [i, [i + 1, i + 2]])
      
      // 测试不同扁平化方法的一致性
      const methods = [
        () => Flatten.flatten(arr),
        () => Flatten.flattenDeep(arr),
        () => Flatten.flattenDepth(arr, 2),
        () => Flatten.flattenImmutable(arr),
        () => Flatten.flattenWithMap(arr, (item) => item * 2, 1)
      ]
      
      const results = methods.map(method => {
        const start = performance.now()
        const result = method()
        const end = performance.now()
        return {
          time: end - start,
          length: result.length
        }
      })
      
      return results.every(result => result.time < 100 && result.length > 0)
    })()
  )
}

testFlattenPerformance()
